package com.ruoyi.project.bajiaostar.tUserAmount.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.bajiaostar.tUserAmountDetail.domain.TUserAmountDetail;
import com.ruoyi.project.bajiaostar.tUserAmountDetail.service.ITUserAmountDetailService;
import com.ruoyi.project.user.domain.TUser;
import com.ruoyi.project.user.service.ITUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.bajiaostar.tUserAmount.mapper.TUserAmountMapper;
import com.ruoyi.project.bajiaostar.tUserAmount.domain.TUserAmount;
import com.ruoyi.project.bajiaostar.tUserAmount.service.ITUserAmountService;
import com.ruoyi.common.utils.text.Convert;
import org.springframework.util.ObjectUtils;

/**
 * 用户账户Service业务层处理
 * 
 * @author wangwu
 * @date 2024-05-23
 */
@Service
public class TUserAmountServiceImpl implements ITUserAmountService 
{
    @Autowired
    private TUserAmountMapper tUserAmountMapper;
    @Autowired
    ITUserService userService;
    @Autowired
    ITUserAmountDetailService userAmountDetailService;


    /**
     * 查询用户账户
     * 
     * @param id 用户账户ID
     * @return 用户账户
     */
    @Override
    public TUserAmount selectTUserAmountById(Long id)
    {
        return tUserAmountMapper.selectTUserAmountById(id);
    }

    /**
     * 查询用户账户
     *
     * @param userId 用户账户ID
     * @return 用户账户
     */
    @Override
    public TUserAmount selectTUserAmountByUserId(Long userId) {
        return tUserAmountMapper.selectTUserAmountByUserId(userId);
    }

    /**
     * 查询用户账户列表
     * 
     * @param tUserAmount 用户账户
     * @return 用户账户
     */
    @Override
    public List<TUserAmount> selectTUserAmountList(TUserAmount tUserAmount)
    {
        return tUserAmountMapper.selectTUserAmountList(tUserAmount);
    }

    /**
     * 新增用户账户
     * 
     * @param tUserAmount 用户账户
     * @return 结果
     */
    @Override
    public int insertTUserAmount(TUserAmount tUserAmount)
    {
        tUserAmount.setCreateTime(DateUtils.getNowDate());
        return tUserAmountMapper.insertTUserAmount(tUserAmount);
    }

    /**
     * 修改用户账户
     * 
     * @param tUserAmount 用户账户
     * @return 结果
     */
    @Override
    public int updateTUserAmount(TUserAmount tUserAmount)
    {
        tUserAmount.setUpdateTime(DateUtils.getNowDate());
        return tUserAmountMapper.updateTUserAmount(tUserAmount);
    }

    /**
     * 删除用户账户对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTUserAmountByIds(String ids)
    {
        return tUserAmountMapper.deleteTUserAmountByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户账户信息
     * 
     * @param id 用户账户ID
     * @return 结果
     */
    @Override
    public int deleteTUserAmountById(Long id)
    {
        return tUserAmountMapper.deleteTUserAmountById(id);
    }

    /**
     * 更新账户同步信息
     *
     * @return
     */
    @Override
    public String executeAccountTask() {

        //获取所有用户信息
        List<TUser> list = userService.selectTUserList(new TUser());
        if(ObjectUtils.isEmpty(list)){
            return "暂无数据可执行";
        }
        for (TUser u:list){
            //获取所有资金明细
            TUserAmountDetail detail=new TUserAmountDetail();
            detail.setUserId(u.getId());
            List<TUserAmountDetail> detailList = userAmountDetailService.selectTUserAmountDetailList(detail);
            if(ObjectUtils.isEmpty(detailList)){
              continue;
            }
            //获取所有待结算金额
            int stayAccount = 0;
            //获取所有待结算金额
            int pzAccount = 0;
            //获取所有待结算金额
            int incomeAmount = 0;
            for (TUserAmountDetail de:detailList){
                if(de.getDetailType().equals(1)){
                    pzAccount=pzAccount+de.getPrice();
                }else{
                    stayAccount=stayAccount+de.getStayPayAmount();
                    incomeAmount=incomeAmount+de.getSurplusAmount();
                }
            }
            TUserAmount amount1 = tUserAmountMapper.selectTUserAmountByUserId(u.getId());
            if(ObjectUtils.isEmpty(amount1)){
                continue;
            }
            amount1.setIncomeAmount(incomeAmount);
            amount1.setStayPayAmount(-stayAccount+pzAccount);
            tUserAmountMapper.updateTUserAmount(amount1);
        }
        return "成功";
    }
}
